//
//  UIImage+Scale.swift
//  PhotoSelector
//
//  Created by newuser on 15/12/30.
//  Copyright © 2015年 newuser. All rights reserved.
//

import UIKit

extension UIImage {

    ///返回新的照片
    func scaleImage() -> UIImage {
        let newWidth:CGFloat = 300
        
        if self.size.width < newWidth {
        
           return self
        }
        
        let newHeight = newWidth * size.width / size.height
        
        let newSize = CGSize(width:newWidth , height: newHeight)
        
        //开启上下文
        UIGraphicsBeginImageContext(newSize)
        
        //将图片绘制
        drawInRect(CGRect(origin: CGPointZero, size: newSize))
        
        //获得上下文
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭
        UIGraphicsEndImageContext()
        
        //返回
        return newImage
    
    }


}

